package com.pengcheng.taskapplication.mvp.view.fragment;

import android.support.v4.app.Fragment;

/**
 * Created by Think on 2017/3/10 0010.
 */
public abstract class BaseLazyFragment extends Fragment {
    /**
     * Fragment当前状态是否可见
     */
    protected boolean isVisible;


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //相当于Fragment的onResume
            onFragmentResume();
        } else {
            //相当于Fragment的onPause
            onFragmentPause();
        }
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoad();
    }


    /**
     * 不可见
     */
    protected void onInvisible() {
        onFragmentResume();
    }

    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad();

    protected abstract void onFragmentPause();

    protected abstract void onFragmentResume();
}
